package mn.idax.exchange.otc.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mn.idax.exchange.auth.bean.AuthInfo;
import mn.idax.exchange.auth.util.AuthInfoUtils;
import mn.idax.exchange.common.bean.ResponseData;
import mn.idax.exchange.otc.constant.RequestUrlConstant;
import mn.idax.exchange.otc.domain.*;
import mn.idax.exchange.otc.entity.OTCUser;
import mn.idax.exchange.otc.enums.CommentTypeEnum;
import mn.idax.exchange.otc.enums.response.CommentResponseEnum;
import mn.idax.exchange.otc.enums.response.UserResponseEnum;
import mn.idax.exchange.otc.exception.IdaxException;
import mn.idax.exchange.otc.service.OTCCommentService;
import mn.idax.exchange.otc.service.OTCUserService;
import mn.idax.exchange.otc.vo.GetCommentPageDTO;
import mn.idax.exchange.otc.vo.UserBeCommentResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;


/**
 * Create by jack_huang on 2018/8/6
 * 信用评价
 */
@Api(value = "信用评价",tags={"信用评价"})
@RestController
@RequestMapping(RequestUrlConstant.MODULE_COMMENT)
public class CommentController {

    @Autowired
    private OTCCommentService otcCommentService;

    @Autowired
    private OTCUserService otcUserService;

    //评价订单
    @PostMapping(value = RequestUrlConstant.COMMENT_CREATE)
    @ApiOperation(value = "评价订单", notes = "评价订单,(jack_huang，自测通过)")
    public ResponseData commentOrder(@RequestBody CommentRequestDTO commentRequestDTO){

        //获取登录用户信息
        AuthInfo authInfo = AuthInfoUtils.getCurrent();
        Integer userId = Optional.ofNullable(authInfo).map(a -> a.getId()).orElse(-1);
        String orderNum = commentRequestDTO.getOrderNum();
        if (StringUtils.isEmpty(orderNum)){
            throw new IdaxException(CommentResponseEnum.ORDERID_ISNULL.getRespCode());
        }
        Integer commentType = commentRequestDTO.getCommentType();
        if (CommentTypeEnum.getCode(commentType) == null){
            throw new IdaxException(CommentResponseEnum.COMMENT_TYPE_ERROR.getRespCode());
        }

        String content = commentRequestDTO.getContent();

        if (!otcCommentService.createComment(orderNum, commentType, content, userId)) {
            throw new IdaxException(CommentResponseEnum.COMMENT_CREATEFAIL.getRespCode());
        }
        return ResponseData.success();
    }

    //删除评价
    @PostMapping(RequestUrlConstant.COMMENT_DELETE)
    @ApiOperation(value = "删除评价", notes = "删除评价,(jack_huang，自测通过)")
    public ResponseData commentDelete(@RequestBody CommentBaseDTO commentBaseDTO) {

        //获取登录用户信息
        AuthInfo authInfo = AuthInfoUtils.getCurrent();
        Integer userId = Optional.ofNullable(authInfo).map(a -> a.getId()).orElse(-1);
        String orderNum = commentBaseDTO.getOrderNum();
        if (StringUtils.isEmpty(orderNum)) {
            throw new IdaxException(CommentResponseEnum.ORDERID_ISNULL.getRespCode());
        }
        if (!otcCommentService.deleteComment(orderNum, userId)) {
            throw new IdaxException(CommentResponseEnum.COMMENT_ISNOTEXIST.getRespCode());
        }
        return ResponseData.success();
    }

    //修改评价
    @PostMapping(RequestUrlConstant.COMMENT_UPDATE)
    @ApiOperation(value = "修改评价", notes = "修改评价,(jack_huang，自测通过)")
    public ResponseData commentUpdate(@RequestBody CommentUpdateDTO commentUpdateDTO) {
        //获取登录用户信息
        AuthInfo authInfo = AuthInfoUtils.getCurrent();
        Integer userId = Optional.ofNullable(authInfo).map(a -> a.getId()).orElse(-1);
        String orderNum = commentUpdateDTO.getOrderNum();
        Integer commentType = commentUpdateDTO.getCommentType();
        if (CommentTypeEnum.getCode(commentType) == null){
            throw new IdaxException(CommentResponseEnum.COMMENT_TYPE_ERROR.getRespCode());
        }

        if (!otcCommentService.updateCommentLevel(orderNum, commentType, userId,commentUpdateDTO.getContent())) {
            throw new IdaxException(CommentResponseEnum.COMMENT_ISNOTEXIST.getRespCode());
        }
        return ResponseData.success();
    }

    //评价列表
    @PostMapping(RequestUrlConstant.COMMENT_LIST)
    @ApiOperation(value = "评价列表", notes = "评价列表,(jack_huang，自测通过)")
    public ResponseData getComment(@RequestBody CommentPageParamsDTO pageParams) {

        //获取被评价人的id
        OTCUser otcUser = otcUserService.getOtcUserByUniqueKey(pageParams.getUniqueKey());
        if (otcUser == null){
            throw new IdaxException(UserResponseEnum.USER_ISNOTEXIST.getRespCode());
        }
        Integer userId = otcUser.getUserid();
        Integer pageIndex = pageParams.getPageIndex();
        Integer pageSize = pageParams.getPageSize();

        if (pageIndex == null || pageIndex < 0 ) {
            pageIndex = 0;
        }
        //防止用户恶意攻击
        if (pageSize == null || pageSize <= 0 || pageSize > 1000) {
            pageSize = 1000;
        }

        //封装查询参数
        GetCommentPageDTO getCommentPageDTO = new GetCommentPageDTO(userId, pageParams.getIsContext(),pageParams.getCommentType(),pageIndex, pageSize);

        CommentShowResultDTO commentShowResult = otcCommentService.getCommentPage(getCommentPageDTO);
        return ResponseData.success(commentShowResult);
    }




    //评价列表
    @PostMapping(RequestUrlConstant.COMMENT_OTHERSTOME)
    @ApiOperation(value = "别人对我的评价", notes = "别人对我的评价,(jack_huang，自测通过)")
    public ResponseData othersCommentMe() {

        //获取登录用户信息
        AuthInfo authInfo = AuthInfoUtils.getCurrent();
        Integer userId = Optional.ofNullable(authInfo).map(a -> a.getId()).orElse(-1);
        List<UserBeCommentResult> results = otcCommentService.getOthersCommentMe(userId);
        return ResponseData.success(results);
    }


}
